import $ from 'jquery'

export function turnFuc() {
  // 翻页插件初始化配置
  $('#book').turn({
    display: 'single',
    acceleration: true,
    gradients: true,
    elevation: 50,
    duration: 2000,
    direction: 'ltr',
    when: {
      turned: function (e, page) {
        console.log('current page is', page)
      }
    }
  })

  // 禁用默认翻页效果
  $('#book').turn('disable', true)

  // 触摸滑动翻页
  let startX, moveEndX, startY, moveEndY

  $('#book').on('touchstart', function (e) {
    startX = e.originalEvent.changedTouches[0].pageX
    startY = e.originalEvent.changedTouches[0].pageY
  })

  $('#book').on('touchend', function (e) {
    moveEndX = e.originalEvent.changedTouches[0].pageX
    moveEndY = e.originalEvent.changedTouches[0].pageY

    let X = moveEndX - startX
    let Y = moveEndY - startY

    if ((window.isPortrait && Y > 50) || (!window.isPortrait && X > 50)) {
      $('#book').turn('previous')
    } else if (
      (window.isPortrait && Y < -50) ||
      (!window.isPortrait && X < -50)
    ) {
      $('#book').turn('next')
    }
  })
}
